function withData(param) {
  return param < 10 ? "0" + param : "" + param;
}

function getLoopArray(start, end) {
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    array.push(withData(i));
  }
  return array;
}

function getMonthDay(year, month) {
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
    array = null;

  switch (month) {
    case "01":
    case "03":
    case "05":
    case "07":
    case "08":
    case "10":
    case "12":
      array = getLoopArray(1, 31);
      break;
    case "04":
    case "06":
    case "09":
    case "11":
      array = getLoopArray(1, 30);
      break;
    case "02":
      array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28);
      break;
    default:
      array = "月份格式不正确，请重新输入！";
  }
  return array;
}

function getNewDateArry() {
  // 当前时间的处理 年月日时分秒
  var newDate = new Date();
  var year = withData(newDate.getFullYear()),
    mont = withData(newDate.getMonth() + 1),
    date = withData(newDate.getDate()),
    hour = withData(newDate.getHours()),
    minu = withData(newDate.getMinutes());
  // seco = withData(newDate.getSeconds());

  return [year, mont, date, hour, minu];
}

function dateTimePicker(startYear, endYear, date) {
  // 返回默认显示的数组和联动数组的声明
  var dateTime = [],
    dateTimeArray = [[], [], [], [], []];
  var start = startYear || 1978;
  var end = endYear || 2100;

  //处理传过来的字符串转化为数组
  let dataArr = date.split(" ")[0].split("/");
  let time = date.split(" ")[1].split(":");
  // 默认开始显示数据 如果把自定义值传过来的话就使用自定义时间 否则用当前时间  ...dataArr数组解构
  var defaultDate = date ? [...dataArr, ...time] : getNewDateArry();

  // 处理联动列表数据
  /*年月日 时分秒*/
  dateTimeArray[0] = getLoopArray(start, end);
  dateTimeArray[1] = getLoopArray(1, 12);
  dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
  dateTimeArray[3] = getLoopArray(0, 23);
  dateTimeArray[4] = getLoopArray(0, 59);
  // dateTimeArray[5] = getLoopArray(0, 59);

  //遍历dateTimeArray数组
  dateTimeArray.forEach((current, index) =>
    //匹配defaultDate[index]值在current数组中的位置
    dateTime.push(current.indexOf(defaultDate[index]))
  );

  return {
    dateTimeArray: dateTimeArray,
    dateTime: dateTime
  };
}
function getDateDiff(dateTimeStamp) {
  // #ifdef APP
  // if (plus.os.name == "iOS") {
  //   dateTimeStamp = dateTimeStamp.replaceAll("-", "/");
  // }
  // #endif
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var now = new Date().getTime();
  var dateTime = new Date(dateTimeStamp).getTime();
  var diffValue = now - dateTime;
  if (diffValue < 0) {
    return;
  }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  var result = "";
  if (monthC >= 1) {
    //x月x日
    result = gettimeZY(dateTimeStamp);
  } else if (weekC >= 1) {
    //x月x日
    result = gettimeZY(dateTimeStamp);
  } else if (parseInt(dayC) == 1) {
    result = "昨天";
  } else if (parseInt(dayC) > 1) {
    //x月x日
    result = gettimeZY(dateTimeStamp, 1);
  } else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前";
  } else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前";
  } else result = "刚刚";
  return result;
}
//转译
function gettimeZY(date) {
  var newDate = new Date(date);
  var year = newDate.getFullYear();
  var mont = withData(newDate.getMonth() + 1);
  var date = withData(newDate.getDate());
  var hour = withData(newDate.getHours());
  var minute = withData(newDate.getMinutes());
  var second = withData(newDate.getSeconds());
  var result = "";
  var nDate = new Date();
  var year2 = nDate.getFullYear();

  if (year2 > year) {
    result = year + "年" + mont + "月" + date + "日";
  } else {
    result = mont + "月" + date + "日";
  }
  return result;
}

// 获取当前时间
function getTimes() {
  var newDate = new Date();
  var year = newDate.getFullYear();
  var mont = withData(newDate.getMonth() + 1);
  var date = withData(newDate.getDate());
  var hour = withData(newDate.getHours());
  var minute = withData(newDate.getMinutes());
  var second = withData(newDate.getSeconds());
  var result =
    year + "-" + mont + "-" + date + " " + hour + ":" + minute + ":" + second;
  return result;
}
export default {
  dateTimePicker: dateTimePicker,
  getMonthDay: getMonthDay,
  getDateDiff: getDateDiff,
  getTimes: getTimes
};
// module.exports = {
// 	dateTimePicker: dateTimePicker,
// 	getMonthDay: getMonthDay,
// 	getDateDiff:getDateDiff,
// 	getTimes:getTimes
// }
